import { BT_State } from "../../BTDefine";
import { Composite } from "./Composite";

export class Selector extends Composite {
    protected _curIndex = 0;

    public onEnter(): void {
        this._curIndex = 0;
    }

    public onExit(): void {

    }

    onProcess(dt: number) {
        let startIndex = this._curIndex;

        let finish = false;

        for (let i = startIndex; i < this._children.length; i++) {
            const child = this._children[i];
            let ret = child.tick(dt);

            if (ret == BT_State.SUCCESS) {
                this.setResult(BT_State.SUCCESS);
                break;
            }

            if (ret == BT_State.RUNNING) {
                break;
            }

            this._curIndex++;
        }

        if (this._state == BT_State.RUNNING && this._curIndex >= this._children.length) {
            this.setResult(BT_State.FAILURE);
        }
    }
}